<?php
/**
 * 付款单管理
 */
namespace app\admin\controller;

use think\Request;
use app\admin\service\Payment as PaymentService;
use app\admin\validate\Payment as PaymentValidate;
use app\common\Common;

class Payment extends BaseController
{
    /**
     * 获取采购单列表
     * @param Request $request
     * @param PaymentService $PaymentService
     * @param PaymentValidate $validate
     * @return \think\response\Json
     */
    public function getList(Request $request,PaymentService $PaymentService,PaymentValidate $validate,Common $oCommon)
    {
//        $oCommon->storeLog(1,2);
        $checkData = $request->param();
        if (!$validate->scene('search')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }

        $param['start_date'] =  $request->param('start_date',null);
        $param['end_date'] =  $request->param('end_date',null);
        $param['payno'] =  $request->param('payno',null);
        $param['purchase_no'] =  $request->param('purchase_no',null);
        $param['status'] =  $request->param('status',null);

        $intPage =  $request->param('pageIndex',1);
        $intPageSize =  $request->param('pageSize',50);
        $arrData = $PaymentService->getList($param,(int)$intPage,(int)$intPageSize);
        return json($arrData);
    }

    /**
     * 采购单添加和修改
     * @param Request $request
     * @param PaymentService $PaymentService
     * @param PaymentValidate $validate
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request,PaymentService $PaymentService,PaymentValidate $validate){
        $checkData = $request->param();
        $checkType = isset($checkData['id']) ? 'save' : 'create';
        if (!$validate->scene($checkType)->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['operator_id'] = $request->operator_id;

        $param['pay_amount'] = $request->param('pay_amount',null);
        $param['purchaseid'] = $request->param('purchaseid',null);
        $param['owner'] = $request->param('owner',null);
        $param['payment_date'] = $request->param('payment_date',null);
        $param['payment_account'] = $request->param('payment_account',null);
        $param['pay_type'] = $request->param('pay_type',null);
        $param['recipient_account'] = $request->param('recipient_account',null);
        $param['remark'] = $request->param('remark',null);
        $param['attachment'] = $request->param('attachment',null);
        $param['responsible'] = $request->param('responsible',null);

        $param['id'] =  $request->param('id',null);
        $arrData = $PaymentService->save($param);
        return json($arrData);
    }

    /**
     * 采购单详情
     * @param Request $request 采购单ID
     * @return \think\response\Json 返回数据
     * @author huangfeng
     * @date 2024-6-28
     */
    public function getInfo(Request $request,PaymentService $PaymentService,PaymentValidate $validate)
    {
        $checkData = $request->param();
        if (!$validate->scene('show')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $arrData = $PaymentService->getInfo($param);
        return json($arrData);
    }

    /**
     * 删除单条或多条数据
     * @param Request $request
     * @param PaymentValidate $validate
     * @param PaymentService $PaymentService
     * @return \think\response\Json
     */
    public function delete(Request $request,PaymentValidate $validate,PaymentService $PaymentService)
    {
        $checkData = $request->param();
        if (!$validate->scene('delete')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['ids'] = $request->param('ids',null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $PaymentService->delete($param);
        return json($arrData);
    }

    public function getFieldEnum(Request $request,PaymentService $PaymentService)
    {
        $param = $request->param('field',null);
        $arrData = $PaymentService->getFieldEnum($param);
        return json($arrData);
    }

    /**
     * 采购单审核
     * @param Request $request
     * @param PaymentService $PaymentService
     * @param PaymentValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function examine(Request $request,PaymentService $PaymentService,PaymentValidate $validate){
        $checkData = $request->param();
        if (!$validate->scene('examine')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $param['status'] = $request->param('status',null);
        $param['operator_id'] = $request->operator_id;
        $arrData = $PaymentService->examine($param);
        return json($arrData);
    }

    /**
     * 上传支付凭证
     * @param Request $request
     * @param PaymentService $PaymentService
     * @param PaymentValidate $validate
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function proof(Request $request,PaymentService $PaymentService,PaymentValidate $validate){
        $checkData = $request->param();
        if (!$validate->scene('proof')->check($checkData)) {
            return json(['code' => 1, 'message' => $validate->getError()]);
        }
        $param['id'] = $request->param('id',null);
        $param['proof'] = $request->param('proof',null);
        $param['hash'] = $request->param('hash',null);
        $arrData = $PaymentService->proof($param);
        return json($arrData);
    }

}

